<template>
<div class="container">
  <div class="header">
    <header-index :idx="1"></header-index>
  </div>
  <div class="login-panel">
    <el-card class="box-card">
      <div slot="header" class="clearfix title">
        <span>管理后台登录</span>
      </div>
      <el-form :model="admin" :rules="rules" status-icon  ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="账号" prop="account">
          <el-input v-model="admin.account" type="text" autocomplete="off" size="medium"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="admin.password" :type="viewPass?'passWord':'text'"  autocomplete="off" size="medium"></el-input>
           <i class="el-icon-view myIconView" @click="viewPass = !viewPass"></i>
        </el-form-item>
        <el-form-item>
          <el-checkbox class="mr-10" v-model="admin.rember">记住我</el-checkbox>
          <el-button type="primary" class="ml-10" size="small" @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
     </el-form>
    </el-card>
  </div>
</div>
  
</template>

<script>
import HeaderIndex from '@/layout/HeaderIndex.vue'
import { set_local_storage, get_local_storage, remove_local_storage, get_session_storage, set_session_storage } from "../utils/storage"
export default {
  components: {HeaderIndex},
  data () {
    return {
      admin:{
        account: get_local_storage("account") || get_session_storage("account") || "",  // 账号
        password: get_local_storage("password") || get_session_storage("password") || "", // 密码
        rember: get_local_storage("rember")==="1" || false,
      },
      viewPass: true,
      rules: {
          account: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
          ]
      }
    }
  },
  methods: {
     submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.handlerLogin()
          } else {
            this.$notify.error({
              title: '错误',
              message: "请输入账号密码",
            });
            return false;
          }
        });
     },

     async handlerLogin() {
        let {data} = await this.$req.post({
          url: "/login",
          data: {
            account: this.admin.account,
            password: this.admin.password
          }
        })
        if (data.code === 200) {
          this.$store.commit("user/SET_TOKEN", data.data.token);
          this.$store.commit("user/SET_ID", data.data.id);
          this.$store.commit("user/SET_ACCOUNT", data.data.account);

          if (this.admin.rember) {
            set_local_storage("account", this.admin.account);
            set_local_storage("password", this.admin.password);
            set_local_storage("rember", this.admin.account?1:0);
            set_local_storage("token", data.data.token);
          } else {
            remove_local_storage("account")
            remove_local_storage("password")
            remove_local_storage("rember")
            set_session_storage("account", this.admin.account);
            set_session_storage("password", this.admin.password);
            set_session_storage("token", data.data.token);
          }

          this.$notify({
            title: '成功',
            message: data.msg,
            type: 'success'
          });
          this.$router.push({
            path:"/dashboard"
          })
        }else {
          this.$notify.error({
            title: '错误',
            message: data.msg,
          });
        }
     }


  }
}
</script>

<style lang="scss" scoped>
.header {
    background-color: blue;
}
  .login-panel {
    height: 80vh;
    display:flex;
    justify-content: center;
    align-items: center;
    .title {
      text-align: center;
      font-size: 2rem;
    }
    ::v-deep .el-input__inner {
      width: 80%;
    }
    .myIconView {
      position: absolute;
      right: 26%;
      top: 14px;
      font-size: 1rem;
      opacity: 0.5;
      cursor: pointer;
    }
  }
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card {
    max-width: 480px;
    width: 100%;
  }
</style>